package com.grape.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.grape.dao.BookmarkRepository;
import com.grape.dao.Bookmark_Box_Repository;
import com.grape.dao.UserRepository;
import com.grape.pojo.Bookmark;
import com.grape.pojo.Bookmark_Box;
import com.grape.pojo.User;
import com.grape.service.Bookmark_Box_Service;


@Service
@Transactional
public class Bookmark_Box_ServiceImpl implements Bookmark_Box_Service{

	@Autowired
	private Bookmark_Box_Repository bbr;
	
	@Autowired
	private BookmarkRepository br;
	
	@Autowired
	private UserRepository ur;
	
	@Override
	public void addBookmark(Bookmark_Box model, String bname, String burl) {
		
		Bookmark_Box bb = bbr.findByBbname(model.getBbname());
		if (bb != null) {
			br.save(new Bookmark(bname, burl, bb));
			
		}else {
			User user = ur.findOne(model.getUser().getId());
			model.setUser(user);
			bb = bbr.save(model);
			Bookmark b = new Bookmark(bname, burl, model);
			b.setBookmark_box(bb);
			br.save(b);
			
		}
		
		
	}

	@Override	// 修改书签盒子的名字
	public void editBbname(Bookmark_Box model) {
		Bookmark_Box bb = bbr.findOne(model.getId());
		bb.setBbname(model.getBbname());
	}

	@Override
	public void delBb(Integer id) {
		bbr.delete(id);
	}
	
	
	
	
}
